<?php
namespace Forum\Controller;
use Think\Controller;
class StatisticsController extends CommonController {
    private $stat_range_days=7; 
    private $stat_range_days_times=array();


    //计算要统计的天数
    private function cal_stat_range()
    {
        for($i=0;$i<$this->stat_range_days;$i++)
        {
            $this->stat_range_days_times[$i]['btime'] = mktime(0,0,0,date('m'),date('d')-$i,date('Y'));
            $this->stat_range_days_times[$i]['etime'] = mktime(0,0,0,date('m'),date('d')-$i+1,date('Y'));
            $this->stat_range_days_times[$i]['date'] = date("Y-m-d",$this->stat_range_days_times[$i]['btime']);
        }


    }

    public function index(){

        $this->cal_stat_range();
        print_r($this->stat_range_days_times);
    }
    
    /*
     * 用户数据
     */
    public function users() {
       
        /*
         * 得到要计算的天数
         * 然后数据传入到 前端的 chart.js 里去
         * categories , data 两个参数
         * 
         */
        $this->cal_stat_range();
        
        foreach ($this->stat_range_days_times as $key => $value) {
           $categories .= "'".$value['date']."',";
           $user_count[$key] = $this->_cal_user_in_range($value['btime'], $value['etime']);
           $data .= $user_count[$key].",";
        }
        
        $categories = substr($categories, 0,-1);
        $data = substr($data, 0,-1);
        
        
        $this->assign("categories", $categories);
        $this->assign("data", $data);
        
        $this->display();
    }
    
    
    
    /*
     * 主题数据
     */
    public function threads() {
        /*
         * 得到要计算的天数
         * 然后数据传入到 前端的 chart.js 里去
         * categories , data 两个参数
         * 
         */
        $this->cal_stat_range();
        
        foreach ($this->stat_range_days_times as $key => $value) {
           $categories .= "'".$value['date']."',";
           $user_count[$key] = $this->_cal_thread_in_range($value['btime'], $value['etime']);
           $data .= $user_count[$key].",";
        }
        
        $categories = substr($categories, 0,-1);
        $data = substr($data, 0,-1);
        
        
        $this->assign("categories", $categories);
        $this->assign("data", $data);
        
       $this->display(); 
    }
    
    /*
     * 帖子数据
     */
    public function posts() {
        
        /*
         * 得到要计算的天数
         * 然后数据传入到 前端的 chart.js 里去
         * categories , data 两个参数
         * 
         */
        $this->cal_stat_range();
        
        foreach ($this->stat_range_days_times as $key => $value) {
           $categories .= "'".$value['date']."',";
           $user_count[$key] = $this->_cal_post_in_range($value['btime'], $value['etime']);
           $data .= $user_count[$key].",";
        }
        
        $categories = substr($categories, 0,-1);
        $data = substr($data, 0,-1);
        
        
        $this->assign("categories", $categories);
        $this->assign("data", $data);
        
       $this->display(); 
    }
    
    /*
     * 主题阅读量
     */
    public function thread_reading() {
        $param = "views";
        
        $res = $this->_cal_thread_top_data(20,$param);
        

        $this->assign("results", $res);

        $this->display();
    }
    
    /*
     * 主题回复量
     */
    public function thread_reply() {
        $DB = M("forum_threads");

        $res = $DB->where($map)->order("replies desc")->select();

        foreach ($res as $key => $value) {
        	//举报记录
        	$res[$key]['nickname']=  $this->get_user_nickname_by_userid($value['userid']);
        }

        $this->assign("results", $res);

        $this->display();
    }
    
    /*
     * 主题收藏量
     */
    public function thread_collection() {
        $DB = M("forum_threads");

        $res = $DB->where($map)->order("favtimes desc")->select();

        foreach ($res as $key => $value) {
        	//举报记录
        	$res[$key]['nickname']=  $this->get_user_nickname_by_userid($value['userid']);
        }

        $this->assign("results", $res);

        $this->display();
    }
    /*
     * 主题点赞量
     */
    public function thread_like() {
        $DB = M("forum_threads");

        $res = $DB->where($map)->order("liketimes desc")->select();

        foreach ($res as $key => $value) {
        	//举报记录
        	$res[$key]['nickname']=  $this->get_user_nickname_by_userid($value['userid']);
        }

        $this->assign("results", $res);

        $this->display();
    }
    
    /*
     * 主题分享
     */
    public function thread_share() {
        $DB = M("forum_threads");

        $res = $DB->where($map)->order("sharetimes desc")->select();

        foreach ($res as $key => $value) {
        	//举报记录
        	$res[$key]['nickname']=  $this->get_user_nickname_by_userid($value['userid']);
        }

        $this->assign("results", $res);

        $this->display();
    }
    
    /*
     * 主题举报量
     */
    public function thread_tipoff() {
//        $DB = M("forum_threads");
//
//        $res = $DB->where($map)->order("views desc")->select();
//
//        foreach ($res as $key => $value) {
//        	//举报记录
//        	$res[$key]['nickname']=  $this->get_user_nickname_by_userid($value['userid']);
//        }
//
//        $this->assign("results", $res);
//
//        $this->display();
    }
    
    /*
     * 计算特定时间范围的新增用户
     */
    private function _cal_user_in_range($btime,$etime) {
        $map['ctime'] = array(array('lt',$etime),array('gt',$btime));
        
//        是否小马
//        $map['isprivate']=0;
        
        $DB = M("forum_users");
        $res = $DB->where($map)->count();
        
        return $res;
        
    }
    
    /*
     * 计算特定时间范围的新增主题
     */
    private function _cal_thread_in_range($btime,$etime) {
        $map['ctime'] = array(array('lt',$etime),array('gt',$btime));
        
//        是否小马
//        $map['isprivate']=0;
        
        $DB = M("forum_threads");
        $res = $DB->where($map)->count();
        
        return $res;
        
    }
    
    /*
     * 计算特定时间范围的新增回复帖子
     */
    private function _cal_post_in_range($btime,$etime) {
        $map['ctime'] = array(array('lt',$etime),array('gt',$btime));
        
//        是否小马
//        $map['isprivate']=0;
        
        $DB = M("forum_posts");
        $res = $DB->where($map)->count();
        
        return $res;
        
    }
    
    /*
     * 计算主题top20
     */
    private function _cal_thread_top_data($top_num=20,$param) {
        
        $map['invisible']=0;
//        是否小马
//        $map['isprivate']=0;
        
        $DB = M("forum_threads");
        $res = $DB->where($map)->order("$param".' DESC')->limit($top_num)->select();
        
        foreach ($res as $key => $value) {
        	$res[$key]['nickname']=  $this->get_user_nickname_by_userid($value['userid']);
                $res[$key]['forum_name']=  $this->get_forum_name_by_fid($value['fid']);
        }
        
        return $res;
        
    }

        
}